package com.billsystem.controller;

import com.billsystem.pojo.Account;
import com.billsystem.pojo.Dream;
import com.billsystem.service.DreamService;
import com.billsystem.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class DreamController {

    @Autowired
    private DreamService dreamService;
    @Autowired
    private HttpSession session;


    @PostMapping("/addDream")
    public Object addDream(@RequestBody Dream dream) {
        try{
            int uid = (int)session.getAttribute("userId");
            dream.setUid(uid);
            dreamService.addDream(dream);
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("error");
        }
    }

    @GetMapping("/listDream")
    public Object listDream(){
        int uid = (int)session.getAttribute("userId");
        List<Dream> lists = dreamService.queryListByUid(uid);
        try{
            return Result.success(lists);
        }catch (Exception e){
            return Result.fail("error");
        }
    }


    @PostMapping("/deleteDream")
    public Object deleteDream(@RequestBody Dream dream){
        try{
            int uid = (int)session.getAttribute("userId");
            dream.setUid(uid);
            dreamService.deleteDream(dream);
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("error");
        }
    }

    @PutMapping("/updateDream")
    public Object updateDream(@RequestBody Dream dream){
        try{
            int uid = (int)session.getAttribute("userId");
            dream.setUid(uid);
            dreamService.updateDream(dream);
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("error");
        }
    }

    @PutMapping("/updateState")
    public Object updateState(@RequestBody Dream dream){
        try{
            int uid = (int)session.getAttribute("userId");
            dream.setUid(uid);
            dreamService.updateState(dream);
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("error");
        }
    }
}
